package mypack.threadtest;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockTest {
	static ReadWriteLock rwl = new ReentrantReadWriteLock();
	public static void main(String[] args) {
		// final Output out = new Output();
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					new Output().print("11111111");
				}
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					new Output().print("22222222");
				}
			}
		}).start();

	}

	static class Output {
	
		public void print(String str) {
			rwl.writeLock().lock();
			try {
				int len = str.length();
				for (int i = 0; i < len; i++) {
					System.out.print(str.charAt(i));
				}
				System.out.println();
			} finally {
				rwl.writeLock().unlock();
			}
		}
	}
}
